package com.dsbj.mp.icardservice.controller.msgcenter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.mp.icardservice.controller.BaseController;
import com.dsbj.mp.icardservice.entity.msgcenter.McCate;
import com.dsbj.mp.icardservice.entity.msgcenter.McMessage;
import com.dsbj.mp.icardservice.model.MpUser;
import com.dsbj.mp.icardservice.service.msgcenter.McCateService;
import com.dsbj.mp.icardservice.service.msgcenter.McMessageService;
import com.dsbj.mp.icardservice.utils.strprocess.ObjectParse;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息中心》相关接口。
 * @author cjw 2020-02-21
 */
@RestController
@Transactional
@RequestMapping(value = "/msgcenter")
public class ContrMcMessage extends BaseController {
	
	@Resource
	private McCateService mcCateService;
	
	@Resource
	private McMessageService mcMessageService;
	
	@Resource
	private OutputMcMessage outputMcMessage;
	
	
	
	/**
	 * 获取未读消息总数。
	 * @param
	 * @return
	 * @author cjw 2020-02-24
	 */
	@RequestMapping(value = "/getMcMessageNumNotRead")
	public ResponseMessage getMcMessageNumNotRead(){
		MpUser mpUser=(MpUser) request.getAttribute("wxUser");
		
		List<McMessage> list_mcMessage=mcMessageService.getMcMessageNotRead(mpUser.getIdcardno());
		//返回信息体
		Map<String,Object> mapReturn=new LinkedHashMap<>();
		mapReturn.put("msgNumNotRead",list_mcMessage.size());
		return ResponseMessage.ok(mapReturn);
	}
	
	/**
	 * 获取消息类别。
	 * @param
	 * @return
	 * @author cjw 2020-02-21
	 */
	@RequestMapping(value = "/getMcCate")
	public ResponseMessage getMcCate(){
		MpUser mpUser=(MpUser) request.getAttribute("wxUser");
		
		List<McCate> list_mcCate= mcCateService.getMcCate();
		List<Map<String,Object>> list_mapMcCate= outputMcMessage.generateMcCate(list_mcCate,mpUser.getIdcardno());
		return ResponseMessage.ok(list_mapMcCate);
	}
	
	/**
	 * 根据mcCateId分页获取该类别下的消息列表。
	 * @param
	 * @return
	 * @author cjw 2020-02-21
	 */
	@RequestMapping(value = "/getMcMessageByMcCateIdByPage")
	public ResponseMessage getMcMessageByMcCateIdByPage(String mcCateId, Integer pageNum, Integer pageSize){
		//解析传来的参数
		MpUser mpUser=(MpUser) request.getAttribute("wxUser");
		try{
			mcCateId= ObjectParse.parse_toStr(mcCateId);
			pageNum=ObjectParse.parse_toInt_elseException(pageNum);
			pageSize=ObjectParse.parse_toInt_elseException(pageSize);
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!"+e.toString());
		}
		
		//根据mcCateId分页获取该类别下的消息列表。
		Page<McMessage> page=mcMessageService.getMcMessageByMcCateIdByPage(mcCateId,mpUser.getIdcardno(),pageNum,pageSize);
		Page<Map<String,Object>> pageReturn= OutputMcMessage.generateMcMessageByPage(page);
		return ResponseMessage.ok(pageReturn);
	}
	
	/**
	 * 将消息状态置为"已读"。
	 * 传入参数: mcMessageId。
	 * @param
	 * @return
	 * @author cjw 2020-02-24
	 */
	@RequestMapping(value = "/readMcMessage")
	public ResponseMessage readMcMessage(@RequestBody Map<String,Object> map){
		
		MpUser mpUser=(MpUser) request.getAttribute("wxUser");
		//验证传来的参数
		String mcMessageId=null;
		try{
			mcMessageId=ObjectParse.parse_toStr_elseException(map.get("mcMessageId"));
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!"+e.toString());
		}
		//
		ResponseMessage responseMessage_temp=mcMessageService.updateMcMessageStatus_read(mcMessageId);
		return responseMessage_temp;
	}
	
	/**
	 * 删除mcMessageId对应的消息。
	 * 传入参数: mcMessageId。
	 * @param
	 * @return
	 * @author cjw 2020-02-24
	 */
	@RequestMapping(value = "/delMcMessageById")
	public ResponseMessage delMcMessageById(@RequestBody Map<String,Object> map){
		//验证传来的参数
		MpUser mpUser=(MpUser) request.getAttribute("wxUser");
		String mcMessageId=null;
		try{
			mcMessageId=ObjectParse.parse_toStr_elseException(map.get("mcMessageId"));
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!"+e.toString());
		}
		ResponseMessage responseMessage_temp=mcMessageService.delMcMessageById(mcMessageId);
		return responseMessage_temp;
	}
	
	/**
	 * 一键已读, 将mcCateId下未读消息的状态置为已读。
	 * 传入参数: mcCateId。
	 * @param
	 * @return 
	 * @author cjw 2020-03-06
	 */
	@RequestMapping(value = "/readMcMessageByMcCateId")
	public ResponseMessage readMcMessageByMcCateId(@RequestBody Map<String,Object> map){
		//解析传来的参数
		MpUser mpUser=(MpUser)request.getAttribute("mpUser");
		String mcCateId=null;
		try{
			mcCateId=ObjectParse.parse_toStr_elseException(map.get("mcCateId"));
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!"+e.toString());
		}
		
		/**
		 * 将mcCateId下未读消息的状态置为已读。
		 */
		List<McMessage> list_mcMessage=mcMessageService.getMcMessageByMcCateIdByStatus(mcCateId,1,mpUser.getIdcardno());
		for(McMessage mcMessage: list_mcMessage){
			mcMessage.setStatus(2);
			mcMessage.setUpdateTime(new Date());
		}
		mcMessageService.updateBatchById(list_mcMessage);
		return ResponseMessage.ok();
	}
	
	/**
	 * 一键删除, 删除mcCateId下的消息, type=0时,删除未读消息; type=1时,删除所有消息。
	 * 传入参数: mcCateId,type(默认为0)。
	 * @param
	 * @return
	 * @author cjw 2020-03-06
	 */
	@RequestMapping(value = "/delMcMessageByMcCateId")
	public ResponseMessage delMcMessageByMcCateId(@RequestBody Map<String,Object> map){
		//解析传来的参数
		MpUser mpUser=(MpUser)request.getAttribute("mpUser");
		String mcCateId=null;
		int type=0;
		try{
			mcCateId=ObjectParse.parse_toStr_elseException(map.get("mcCateId"));
			type=ObjectParse.parse_toInt(map.get("type"));
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!"+e.toString());
		}
		
		/**
		 * 删除mcCateId下的消息, type=0时,删除未读消息; type=1时,删除所有消息。
		 */
		if(type==0){
			mcMessageService.delMcMessageReadByMcCateId(mcCateId,mpUser.getIdcardno());
			return ResponseMessage.ok();
		}
		//根据mcCateId,删除该类别下的所有消息。
		if(type==1){
			mcMessageService.delMcMessageByMcCateId(mcCateId,mpUser.getIdcardno());
			return ResponseMessage.ok();
		}
		return ResponseMessage.ok();
	}
	
	
	
}
